//
//  RecommendViewModel.swift
//  DouYuZB
//
//  Created by DSDL on 2016/10/31.
//  Copyright © 2016年 鼎商动力. All rights reserved.
//

import UIKit

class RecommendViewModel: BaseViewModel {
    //MARK: - 懒加载属性
    lazy var cycleModels: [CycleModel] = [CycleModel]()
    fileprivate lazy var bigDataGroup: AnchorGroup = AnchorGroup()
    fileprivate lazy var prettyGroup: AnchorGroup = AnchorGroup()
}

//MARK: - 发送网络请求
extension RecommendViewModel{
    //1.请求推荐数据
    func requestData(_ finishCallback: @escaping() -> Void){
        //1.定义参数
        let parameters = ["limit": "4", "offset": "0", "time": Date.getCurrentTime()]
        
        //2.创建组
        let dgroup = DispatchGroup()
        
        //进入组
        dgroup.enter()
        //1.请求推荐数据
        NetworkTools.requestData(.get, URLString: "http://capi.douyucdn.cn/api/v1/getbigDataRoom", parameters: ["time": Date.getCurrentTime()]) { (result) in
            
            //1.将 result z转为字典
            guard let resultDict = result as? [String: AnyObject] else { return }
            //2.根据data 该key，获取数组
            guard let dataArray = resultDict["data"] as? [[String: AnyObject]] else { return }
            //3.设置组的属性，遍历字典
            //设置组的属性
            self.bigDataGroup.tag_name = "热门"
            self.bigDataGroup.icon_name = "home_header_hot"
            for dict in dataArray{
                let anchor = AnchorModel(dict: dict)
                self.bigDataGroup.anchors.append(anchor)
            }
            //3.离开组
            dgroup.leave()
        }
        
        //2.请求第二部分颜值数据
        dgroup.enter()
       
        NetworkTools.requestData(.get, URLString: "http://capi.douyucdn.cn/api/v1/getVerticalRoom", parameters: parameters) { (result) in
            //1.将 result z转为字典
            guard let resultDict = result as? [String: AnyObject] else { return }
            //2.根据data 该key，获取数组
            guard let dataArray = resultDict["data"] as? [[String: AnyObject]] else { return }
            //3.设置组的属性，遍历字典
            //设置组的属性
            self.prettyGroup.tag_name = "颜值"
            self.prettyGroup.icon_name = "home_header_phone"
            for dict in dataArray{
                let anchor = AnchorModel(dict: dict)
                self.prettyGroup.anchors.append(anchor)
            }
            dgroup.leave()
        }
        
        //3.请求后面部分的游戏数据
        
        dgroup.enter()
        self.loadAnchorData(isGroupdata: true, URL: "http://capi.douyucdn.cn/api/v1/getHotCate", parameters: parameters) {
             dgroup.leave()
        }
        dgroup.notify(queue: DispatchQueue.main) {
            self.anchorGroup.insert(self.prettyGroup, at: 0)
            self.anchorGroup.insert(self.bigDataGroup, at: 0)
            finishCallback()
        }
    }
    
    //2.请求轮播图数据
    func requestCycleData(_ finishCallback:@escaping () -> Void) {
        NetworkTools.requestData(.get, URLString: "http://www.douyutv.com/api/v1/slide/6", parameters: ["version":"2.300"]) { (result) in
//            print(result)
            //1.校验数据
            guard let resultDict = result as? [String: AnyObject] else { return }
            //2.根据data的key 取数据值
            guard let dataArray = resultDict["data"] as? [[String: AnyObject]]  else { return }
            //3.字典转模型
            for dict in dataArray{
                let  cycleModel = CycleModel(dict: dict)
                self.cycleModels.append(cycleModel)
            }
            
            finishCallback()
        }
    }
}
